package system.base;

import java.text.DecimalFormat;
import java.util.ArrayList;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.form.GSOrderDetailQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.page.ResultProcess;
import framework.core.DBConnection;
import framework.core.IHandler;
import framework.core.ILoginContext;
import framework.core.Logger;
import framework.core.SrvContext;

/*
 Author    :chengbiao.he
 CreateTime:2008-01-30 14:21:49
 */
public class OrderDetailSrv extends IHandler {

	public OrderDetailSrv() {
		super();
	}

	private void listOrderDetail(DBConnection con, ILoginContext login,
			SrvContext context, String path) throws Exception {
		GSOrderDetailQueryForm tForm = new GSOrderDetailQueryForm();
		tForm.setRequest(context.m_req);
		
		GSBaseForm aForm = new GSBaseForm();
		aForm.setRequest(context.m_req);
		String[] Month;
		Month = context.getParameterValues("Month");
		String startTime="";
		String endTime="";
		String orderdetailstatus="";
		if (Month == null || Month.length == 0 || Month[0].equals("")) {

		} else if (Month[0].equals("1")) {// 当月
			String tCurDT = com.hitrust.gs.util.date.DateUtil
					.getCurrentDate("yyyy-MM-dd");
			tCurDT = tCurDT.substring(0, tCurDT.length() - 2);
			aForm.getRequest()
					.setAttribute("orderCreateDateStr", tCurDT + "01");
			startTime=tCurDT + "01";
		} else if (Month[0].equals("2")) {// 当天
			String tCurDT = com.hitrust.gs.util.date.DateUtil
					.getCurrentDate("yyyy-MM-dd");
			aForm.getRequest().setAttribute("orderCreateDateEnd", tCurDT);
			startTime=tCurDT;
		}
		String[] orderDetailStatus;
		orderDetailStatus = context.getParameterValues("orderDetailStatus");
		if (orderDetailStatus == null || orderDetailStatus.length == 0 || orderDetailStatus[0].equals("")) {

		} else  {// 当月
			orderdetailstatus=orderDetailStatus[0];
		} 

	
		String tCurDT = com.hitrust.gs.util.date.DateUtil
					.getCurrentDate("yyyy-MM-dd");
		tForm.getRequest().setAttribute("orderCreateDateEnd", tCurDT);
		endTime=tCurDT;
	

		// 取得订单明细状态
		aForm.getRequest().setAttribute("orderdetailstatuslist",
				getOrderDetailStatusList());

		// 取得订单明细状态
		aForm.getRequest().setAttribute("ordertypelist", getOrderTypeList());

		// 取得产品分类
		GSManufactureProcessor tManufProc = new GSManufactureProcessor();
		tManufProc.getManufactureOfLevel(aForm);

		context.setNextUrl(path + "orderdetailquery.jsp?orderCreateDateStr="+startTime+"&orderCreateDateEnd"+endTime+"&orderDetailStatus"+orderdetailstatus);
	}

	/**
	 * 取得订单明细状态
	 * 
	 * @return 订单明细状态列表
	 */
	private ArrayList getOrderDetailStatusList() {
		ArrayList tResult = new ArrayList();

		String[] tArray = new String[2];
		tArray[0] = "6";
		tArray[1] = "已提交";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "7";
		tArray[1] = "已核准";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "8";
		tArray[1] = "未核准";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "9";
		tArray[1] = "订货中";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "10";
		tArray[1] = "已确认";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "11";
		tArray[1] = "已拒绝";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "12";
		tArray[1] = "交货确认";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "14";
		tArray[1] = "已完成";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "21";
		tArray[1] = "已取消";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "22";
		tArray[1] = "已作废";
		tResult.add(tArray);

		return tResult;
	}

	private ArrayList getOrderTypeList() {
		ArrayList tResult = new ArrayList();
		String[] tArray = new String[2];
		tArray[0] = "0";
		tArray[1] = "正常采购";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "1";
		tArray[1] = "应急采购";
		tResult.add(tArray);

		return tResult;
	}

	public void doAction(DBConnection con, SrvContext context,
			ILoginContext loginContext) throws Exception {

		String action_code = context.getActionCode();
		String path = "/system/base/";

		if (action_code.equalsIgnoreCase("listOrderDetail")) {
			listOrderDetail(con, loginContext, context, path);
			return;
		}

		Logger.throwException("LeaderPageSrv", "unknown action_code="
				+ action_code);

	}

	private String[] getPageParams(GSOrderDetailQueryForm aForm)
			throws Exception {
		String tStr[] = new String[3];
		int totalRecords;
		int pageNo;
		int pageSize;
		String tPage = GSBaseConstants.tPage;
		// 根据页面传入的查询参数查询订单信息
		if (aForm.getChoice() == null || aForm.getChoice().equals("")) {
			GSOrderDAO tDAO = new GSOrderDAO();
			totalRecords = tDAO.getOrderDetailListCount(aForm);

			// 取得系统默认的页大小
			pageSize = Integer.parseInt(tPage);
			// PageNo=1
			pageNo = 1;
			int n = totalRecords % pageSize == 0 ? totalRecords / pageSize
					: totalRecords / pageSize + 1;
			if (pageNo > n) {
				pageNo = n;
			}
			tStr[0] = "" + pageNo;
			tStr[1] = "" + pageSize;
			tStr[2] = "" + totalRecords;
		} else {
			tStr = GSBaseManager.getPageNoAndPageSize(aForm);
		}
		return tStr;
	}
}
